package com.project.service.impl;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;

public class BaseService {
    private static SqlSessionFactory factory;
    protected boolean flag = true;//判断是否提交数据库
    protected static SqlSession session =null;
    static{
        try {

            Reader r = Resources.getResourceAsReader("mybatis.cfg.xml");
            factory = new SqlSessionFactoryBuilder().build(r);


             session = getSession();
            //使用代理方式得到Mapper接口实现类对象

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static SqlSession getSession(){
        return factory.openSession();
    }

    public void commitAndClose(){
        if(flag){
            this.session.commit();
        }
            this.session.close();
    }

}
